package cn.kgc.flightsmanagement.controller;

import cn.kgc.flightsmanagement.pojo.Flight;
import cn.kgc.flightsmanagement.pojo.ResultEntity;
import cn.kgc.flightsmanagement.servcie.FlightService;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author niejianhang
 * @create 2020-05-31 11:19
 */
@CrossOrigin("*")
@RestController
public class FligntController {

    @Resource
    FlightService flightService;

    @RequestMapping("/selAll")
    public ResultEntity<PageInfo<Flight>> selAll(@RequestParam(value = "pageNum",defaultValue = "1",required = false) Integer pageNum,
                                                 @RequestParam(value = "pageSize",defaultValue = "3",required = false) Integer pageSize,
                                                 @RequestParam(value = "id1",defaultValue = "0",required = false) Integer id1,
                                                 @RequestParam(value = "id2",defaultValue = "0",required = false) Integer id2){
        PageInfo<Flight> pageInfo = flightService.selAll(pageNum, pageSize, id1, id2);
        return ResultEntity.successWithoutData(pageInfo);
    }

    @RequestMapping("/del")
    public ResultEntity<String> del(Integer id){
        try {
            int del = flightService.del(id);
            return ResultEntity.successWithoutData(del+"");
        } catch (Exception e) {
            return ResultEntity.failed(e.getMessage());
        }

    }

    @RequestMapping("/selById")
    public ResultEntity<Flight> selById(Integer id){

        Flight flight = flightService.selById(id);
        return ResultEntity.successWithoutData(flight);
    }

    @RequestMapping("/save")
    public ResultEntity<String> save(@RequestBody Flight flight){
        try {
            Integer save;
            if(flight.getId()==null){
                save = flightService.add(flight);
            }else{
                save = flightService.update(flight);
            }
            return ResultEntity.successWithoutData(save+"");
        } catch (Exception e) {
            return  ResultEntity.failed(e.getMessage());
        }

    }




}
